package com.web.daonila.service.bussiness;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.web.daonila.message.resp.Article;
import com.web.daonila.message.resp.NewsMessage;
import com.web.daonila.message.resp.TextMessage;
import com.web.daonila.util.MessageUtil;
import com.web.model.BookInfo;
import com.web.service.BookService;

//图文消息
@Component
public class ArticleMessageResp extends MessageRespHandler<TextMessage> implements MessageRespBase {

	Logger logger = LoggerFactory.getLogger(getClass());
	
	@Autowired
	private BookService bookService;
	
	
	public String start(Map<String, String> requestMap) {
		init(requestMap);
		logger.info("TextMessageResp.start():{requestMap={}}", new Object[]{requestMap.toString()});
		return run();
	}

	@Override
	protected String validMessage(Map<String, String> requestMap) {
		String respMessage = "";
        NewsMessage newsMessage = new NewsMessage();  
        newsMessage.setToUserName(fromUserName);  
        newsMessage.setFromUserName(toUserName);  
        newsMessage.setCreateTime(new Date().getTime());  
        newsMessage.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_NEWS);  
        newsMessage.setFuncFlag(0);
        String content = requestMap.get("Content");
        
        
        BookInfo bookInfo = new BookInfo();
        bookInfo.setLimit(6);
        bookInfo.setStart(0);
        bookInfo.setIsBook(1);;
        bookInfo.setBookName(content);
        List<BookInfo> books = bookService.findBooks(bookInfo);
        List<Article> articles = new ArrayList<Article>();
        for (BookInfo book : books) {
        	Article article = new Article();
        	article.setTitle(book.getBookName());
        	article.setDescription(""); 
        	article.setPicUrl("http://0.xiaoqrobot.duapp.com/images/avatar_liu");  
        	article.setUrl("http://blog.csdn.net/lyq8479/article/details/8937622");
        	articles.add(article);
		}
        
        // 设置图文消息个数  
        newsMessage.setArticleCount(articles.size());  
        // 设置图文消息包含的图文集合  
        newsMessage.setArticles(articles);  
        respMessage = MessageUtil.newsMessageToXml(newsMessage);
		return respMessage;
	}
	
}
